<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';

header('Content-Type: text/plain');

$tableCustomer = 'ns_v1customer';
$tableCustomerContact = 'ns_CustomerContact';
$tableMyOrder = 'ns_v1myorder';
$tableMyOrderCancel = 'ns_MyOrderCancel';
$tableMyOrderNote = 'ns_MyOrderNote';
$tableMyOrderPackage = 'ns_MyOrderPackage';
$tablePayment = 'ns_v1payment';
$tableExpense = 'ns_Expense';
$tablePackage = 'ns_v1package';
$tableProduct = 'ns_Product';
$tableSample = 'ns_v1sample';
$tablePurchase = 'ns_v1purchase';
$tablePurchaseItem = 'ns_v1purchaseItem';
$tablePurchaseItemSelect = 'ns_v1purchaseItemSelect';
$tableISource = 'ns_ISource';
$tableICulture = 'ns_ICulture';
$tableIReception = 'ns_IReception';
$tableICeremony = 'ns_ICeremony';
$tableUser = 'ns_v1user';


$tableProduce = 'ns_Produce';
$tableProduceEvent = 'ns_ProduceEvent';
$tableProduceItemMail = 'ns_ProduceItemMail';
$tableProduceItemUser = 'ns_ProduceItemUser';
$tableProduceProduct = 'ns_ProduceProduct';
$tableProduceProductSelect = 'ns_ProduceProductSelect';
$tableProducePurchase = 'ns_ProducePurchase';
$tableProducePurchaseItemSelect = 'ns_ProducePurchaseItemSelect';
$tableProducePurchaseItem = 'ns_ProducePurchaseItem';
$tableProduceTrack = 'ns_ProduceTrack';
$tableProduceTransition = 'ns_ProduceTransition';
$tableProduceUser = 'ns_ProduceUser';


try {
  $hostname = HOST_NAME;
  $username = USER_NAME1;
  $password = PASSWORD1;
  $dbname = DATABASE_NAME1;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $username1 = USER_NAME2;
  $password1 = PASSWORD2;
  $dbname1 = DATABASE_NAME2;
  $myPdo1 = new PDO ('mysql:host=' . $hostname, $username1, $password1);
  $myPdo1->exec('USE ' . $dbname1);
  $myPdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}

$action = MiscUtils::getParam('action', NULL);

try {
  switch ($action) {
    case 'customers':
      customers($myPdo);
      break;     
    case 'ieincomecustomerall':
      ieincomecustomerall($myPdo);
      break;
    case 'ieincomecustomer':
      ieincomecustomer($myPdo);
      break;
    case 'ieincomedaily':
      ieincomedaily($myPdo);
      break;
    case 'expenses':
      expenses($myPdo);
      break;      
    case 'ieexpense':
      ieexpense($myPdo);
      break;
    case 'ieexpenses':
      ieexpenses($myPdo);
      break;
    case 'iesumall':
      iesumall($myPdo);
      break;
    case 'iesumincome':
      iesumincome($myPdo);
      break;
    case 'iesumexpense':
      iesumexpense($myPdo);
      break;
    case 'iestatspackage':
      iestatspackage($myPdo);
      break;
    case 'iestatsproduct':
      iestatsproduct($myPdo);
      break;
    case 'iestatssource':
      iestatssource($myPdo);
      break;
    case 'iestatsculture':
      iestatsculture($myPdo);
      break;
    case 'iestatsreception':
      iestatsreception($myPdo);
      break;
    case 'iestatsreceptioncustomers':
      iestatsreceptioncustomers($myPdo);
      break;
    case 'iestatsceremony':
      iestatsceremony($myPdo);
      break;
    case 'iestatsceremonycustomers':
      iestatsceremonycustomers($myPdo);
      break;
    case 'iecompbymonth':
      iecompbymonth($myPdo);
      break;
    case 'iecompbyyear':
      iecompbyyear($myPdo);
      break;
    case 'iesumexpensedetail':
      iesumexpensedetail($myPdo);
      break;
    case 'deletecus':
      deletecus($myPdo);
      break;
    case 'deleteorder':
      deleteorder($myPdo);
      break;
    case 'deletepayment':
      deletepayment($myPdo);
      break;
      
    case 'deleteexpenses':
      deleteexpenses($myPdo);
      break;      

    case 'payments':
      payments($myPdo);
      break; 

    case 'acctpayment':
      acctpayment($myPdo);
      break;         
      
    case 'paymentdate':
      paymentdate($myPdo);
      break;
    case 'orderdiscount':
      orderdiscount($myPdo);
      break;
    case 'noworkingdateorder':
      noworkingdateorder($myPdo);
      break;
    case 'paymentproblem':
      paymentproblem($myPdo);
      break;
    case 'noweddingday':
      noweddingday($myPdo);
      break;
    case 'noweddingday1':
      noweddingday1($myPdo);
      break;
    case 'paymentproblems':
      paymentproblems($myPdo);
      break;
    case 'paymentproblems1':
      paymentproblems1($myPdo);
      break;
    case 'firstpayment1':
      firstpayment1($myPdo);
      break;
    case 'firstpayment2':
      firstpayment2($myPdo);
      break;
    case 'firstpayment3':
      firstpayment3($myPdo);
      break;
    case 'firstpayment4':
      firstpayment4($myPdo);
      break;
    case 'secondpayment1':
      secondpayment1($myPdo);
      break;
    case 'secondpayment2':
      secondpayment2($myPdo);
      break;
    case 'thirdpayment1':
      thirdpayment1($myPdo, $myPdo1);
      break;
    case 'thirdpayment2':
      thirdpayment2($myPdo, $myPdo1);
      break;
    case 'forthpayment':
      forthpayment($myPdo);
      break;
    case 'smryadmin':
      smryadmin($myPdo, $myPdo1);
      break;
    case 'paymentbonus':
      paymentbonus($myPdo);
      break;
    case 'bonuspaid':
      bonuspaid($myPdo);
      break;
    case 'approvecus':
      approvecus($myPdo);
      break;
    case 'approveorder':
      approveorder($myPdo);
      break;
    case 'approvepayment':
      approvepayment($myPdo);
      break;
    case 'approveexpense':
      approveexpense($myPdo);
      break;
    case 'findByRequest':
      findByRequest($myPdo);
      break;
    case 'weeklywages':
      weeklywages($myPdo);
      break;
    case 'paymentpd':
      paymentpd($myPdo);
      break;
    case 'paymentpd1':
      paymentpd1($myPdo);
      break;
    case 'discount':
      discount($myPdo);
      break;
    case 'discount1':
      discount1($myPdo);
      break;
    case 'wageupdated':
      wageupdated($myPdo);
      break;
    case 'wagesupdated':
      wagesupdated($myPdo);
      break;
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}

function _months($from, $to) {
  $result = array();
  for ($i = $from->year, $il = $to->year; $i <= $il; $i++) {
    for ($j = 1, $jl = 12; $j <= $jl; $j++) {
      if ($i == $from->year && $j < $from->month) {
        continue;
      } else if ($i == $to->year && $j > $to->month) {
        continue;
      }
      $tmp = new stdClass();
      $tmp->month = $j;
      $tmp->year = $i;
      $result[] = $tmp;
    }
  }
  return $result;
}

function _days($year, $month) {
  if ($month == 1) {
    return 31;
  } else if ($month == 2) {
    if ($year % 4 == 0) {
      return 29;
    } else {
      return 28;
    }
  } else if ($month == 3) {
    return 31;
  } else if ($month == 4) {
    return 30;
  } else if ($month == 5) {
    return 31;
  } else if ($month == 6) {
    return 30;
  } else if ($month == 7) {
    return 31;
  } else if ($month == 8) {
    return 31;
  } else if ($month == 9) {
    return 30;
  } else if ($month == 10) {
    return 31;
  } else if ($month == 11) {
    return 30;
  } else if ($month == 12) {
    return 31;
  }
}

function customers($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $date = MiscUtils::getParam('date', NULL);
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $wd = SimpleDate::toStamp(json_decode($date));
  
  $cond = MiscUtils::getParam('cond', NULL);
  
  $condition .= ($date) ? ' AND ((o.date >= \'' . $wd . '\') AND (o.date < \'' . ($wd + 86400) . '\'))' : '';
  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  if ($cond) {
    $condition .= ' AND 1=1';
  } else {
    $condition .= ' AND c.cancelled = 0';
  }
  
  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT DISTINCT c.e_oid AS total FROM $tableCustomer AS c $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();

  $sql = "SELECT DISTINCT c.e_oid AS id, c.createdDate AS createdDate,c.userAccount FROM $tableCustomer AS c $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->createdDate = SimpleDate::fromStamp($tmp->createdDate);
    $tmp->orders = array();
    $sql2 = "SELECT DISTINCT o.e_oid AS id, o.type AS type, o.createdDate AS createdDate, o.flag AS flag, o.date AS date, o.price AS price, o.disposalstatus AS status FROM $tableMyOrder AS o  WHERE o.customerId = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp2->createdDate = ($tmp2->createdDate) ? SimpleDate::fromStamp($tmp2->createdDate) : NULL;
      $tmp2->date = ($tmp2->date) ? SimpleDate::fromStamp($tmp2->date) : NULL;
      $tmp2->disposal = new stdClass();
      $tmp2->disposal->status = 0;
      if ($tmp2->status == 1) {
        $tmp2->disposal->status = 1;
      }
      $tmp2->payments = array();
      $sql3 = "SELECT DISTINCT p.e_oid AS id, p.cancelled AS cancelled, p.amount AS amount, p.method AS method, p.stage AS stage, p.date AS date, p.cancelledDate as cancelledDate, p.cancelledOperator as cancelledOperator, p.cancelledMessage as cancelledMessage FROM $tablePayment AS p  WHERE p.orderId = $tmp2->id";
      $stmt3 = $myPdo->prepare($sql3);
      $stmt3->execute();
      $k = 0;
      while ($k < $stmt3->rowCount()) {
        $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
        $tmp3->date = ($tmp3->date) ? SimpleDate::fromStamp($tmp3->date) : NULL;
        $tmp3->cancelledDate = ($tmp3->cancelledDate) ? SimpleDate::fromStamp($tmp3->cancelledDate) : NULL;
        $tmp2->payments[] = $tmp3;
        $k++;
      }
      $tmp->orders[] = $tmp2;
      $j++;
    }
    $sql2 = "SELECT e_oid,bridename,brideaddrLn1,brideaddrLn2,bridephone,bridemobile,brideemail FROM $tableCustomer AS t  WHERE t.e_oid = $tmp->id ";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->bride = $tmp2;
    }
    $sql2 = "SELECT e_oid,groomname,groomaddrLn1,groomaddrLn2,groomphone,groommobile,groomemail FROM $tableCustomer AS t  WHERE t.e_oid = $tmp->id ";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->groom = $tmp2;
    }
    $sql2 = "SELECT * FROM $tableUser AS u  WHERE u.account = '".$tmp->userAccount."'";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->sales = $tmp2;
    }
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function ieincomecustomerall($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition2 = 'WHERE 1 = 1';
  $matches = array();
  if (preg_match_all('/c.e_oid = \'(.*)\'/m', $condition, $matches)) {
    $condition2 = "WHERE c.e_oid = '" . $matches[1][0] . "'";
  }
  $condition2 .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition2 .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $res = new stdClass();
  $res->customer = 0;
  $res->tolRev = 0;
  $res->receipt = 0;
  $res->anticipated = 0;
  $res->cash = 0;
  $res->noncash = 0;
  $res->noPayment = 0;
  $res->revenue = 0;
  $res->netrev = 0;

  $sql = "SELECT DISTINCT c.e_oid AS total FROM $tableCustomer AS c $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute ();
  $res->customer = $stmt->rowCount();

  $sql = "SELECT DISTINCT c.e_oid AS id FROM $tableCustomer AS c $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute ();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT o.e_oid AS id, o.price AS price, o.disposalstatus AS status FROM $tableMyOrder AS o  WHERE o.customerId = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute ();
    $j = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      if ($tmp2->status == 0) {
        $res->tolRev += $tmp2->price;
      }
      $sql3 = "SELECT DISTINCT p.e_oid AS id, p.amount AS amount, p.method AS method, p.cancelled AS cancelled FROM $tablePayment AS p  WHERE p.orderId = $tmp2->id";
      $stmt3 = $myPdo->prepare($sql3);
      $stmt3->execute ();
      $k = 0;
      while ($k < $stmt3->rowCount()) {
        $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
        if ($tmp3->cancelled  == 0) {
          $res->receipt += $tmp3->amount;
          if ($tmp3->method == 'cash') {
            $res->cash += $tmp3->amount;
          } else {
            $res->noncash += $tmp3->amount;
          }
        }
        $k++;
      }
      $j++;
    }
    $i++;
  }
  $res->anticipated = $res->tolRev - $res->receipt;
  
  $sql = "SELECT DISTINCT c.e_oid AS id FROM $tableCustomer AS c  LEFT JOIN $tableMyOrder AS o ON o.customerId = c.e_oid  LEFT JOIN $tablePayment AS cc on cc.orderId = o.e_oid $condition2 AND c.e_oid =cc.customerId";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $res->noPayment = $res->customer - $stmt->rowCount();
  
  $sql = "SELECT DISTINCT o.e_oid AS id, o.price AS result FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId LEFT JOIN $tablePayment AS cc on cc.orderId = o.e_oid $condition2 AND c.e_oid =cc.customerId";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  $tmp = 0;
  while ($i < $stmt->rowCount()) {
    $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp += $tmp2->result;
    $i++;
  }
  $res->revenue = $res->tolRev - $tmp;
  $res->netrev = $tmp;
  echo json_encode($res);
}

function ieincomecustomer($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $orderType = MiscUtils::getParam('ot', NULL);
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition2 = 'WHERE 1 = 1';
  $condition3 = 'AND 1 = 1';
  $matches = array();
  if (preg_match_all('/c.e_oid = \'(.*)\'/m', $condition, $matches)) {
    $condition2 = "WHERE c.e_oid = '" . $matches[1][0] . "'";
    $condition3 = "WHERE o.e_oid = '" . $matches[1][0] . "'";
  }
  $condition2 .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition2 .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $condition3 .= ($createdFrom) ? ' AND (o.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition3 .= ($createdTo) ? ' AND (o.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
    
  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT DISTINCT c.e_oid AS total FROM $tableCustomer AS c $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $customer = $stmt->rowCount();

  $revenue = 0;
  $sql = "SELECT SUM(o.price) AS result FROM $tableCustomer AS c $condition ";
    //$sql =" SELECT SUM(o.price) AS result FROM $tableMyOrder AS o  WHERE o.disposalstatus = 0 AND o.type = '$orderType' $condition3";
    $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $revenue = ($tmp->result) ? $tmp->result : 0;
  }

  $nonCash = 0;
  $sql = "SELECT SUM(p.amount) AS result FROM $tablePayment AS p LEFT JOIN $tableMyOrder AS o ON o.e_oid = p.orderId  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId $condition2 AND p.cancelled = 0 AND p.method != 'cash' AND o.type = '$orderType'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $nonCash = ($tmp->result) ? $tmp->result : 0;
  }

  $cash = 0;
  $sql = "SELECT SUM(p.amount) AS result FROM $tablePayment AS p  LEFT JOIN $tableMyOrder AS o ON o.e_oid = p.orderId LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId $condition2 AND p.cancelled = 0 AND p.method = 'cash' AND o.type = '$orderType'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $cash = ($tmp->result) ? $tmp->result : 0;
  }

  $wedsum = new stdClass();
  $wedsum->customer = $customer;
  $wedsum->revenue = $revenue;
  $wedsum->receipt = $cash + $nonCash;
  $wedsum->anticipated = $revenue - $cash - $nonCash;
  $wedsum->cash = $cash;
  $wedsum->noncash = $nonCash;
  $presum = new stdClass();
  $presum->customer = $customer;
  $presum->revenue = $revenue;
  $presum->receipt = $cash + $nonCash;
  $presum->anticipated = $revenue - $cash - $nonCash;
  $presum->cash = $cash;
  $presum->noncash = $nonCash;
  $porsum = new stdClass();
  $porsum->customer = $customer;
  $porsum->revenue = $revenue;
  $porsum->receipt = $cash + $nonCash;
  $porsum->anticipated = $revenue - $cash - $nonCash;
  $porsum->cash = $cash;
  $porsum->noncash = $nonCash;
  $engsum = new stdClass();
  $engsum->customer = $customer;
  $engsum->revenue = $revenue;
  $engsum->receipt = $cash + $nonCash;
  $engsum->anticipated = $revenue - $cash - $nonCash;
  $engsum->cash = $cash;
  $engsum->noncash = $nonCash;
  $othsum = new stdClass();
  $othsum->customer = $customer;
  $othsum->revenue = $revenue;
  $othsum->receipt = $cash + $nonCash;
  $othsum->anticipated = $revenue - $cash - $nonCash;
  $othsum->cash = $cash;
  $othsum->noncash = $nonCash;

  $res = new stdClass();
  $res->wed = $wedsum;
  $res->pre = $presum;
  $res->por = $porsum;
  $res->eng = $engsum;
  $res->oth = $othsum;
  echo json_encode($res);
}

function ieincomedaily($myPdo) {
global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
    
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1=1');
  $order = MiscUtils::getParam('o', 'd.date');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;  
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
      
      $res = new stdClass();
      $res->page = $page;
      $res->size = $size;
      $res->order = $order;
      $res->queue = $queue;
      $res->condition = $condition;   
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $res->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  $receipt = 0; $cash = 0; $noncash = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);

    if($tmp->cancelled == 0)
    {
        $receipt += $tmp->amount;
        if ($tmp->method == 'cash') {
          $cash += $tmp->amount;
        } else {
          $noncash += $tmp->amount;
        }
    }
    

    $i++;
  }
      $top = new stdClass();
      $top->receipt = $receipt;
      $top->cash = $cash;
      $top->noncash = $noncash;       
      
      $res->top = $top;
        
   echo json_encode($res);
}

function ieexpense($myPdo) {
  global $tableExpense;
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $res = new stdClass();
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->expense = ($tmp->result) ? $tmp->result : 0;
  }
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1 AND d.method = 'C'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->cash = ($tmp->result) ? $tmp->result : 0;
  }
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1 AND d.method != 'C'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->noncash = ($tmp->result) ? $tmp->result : 0;
  }
  echo json_encode($res);
}

function ieexpenses($myPdo) {
  global $tableExpense;
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'd.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT * FROM $tableExpense AS d WHERE $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tableExpense AS d WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->createdDate = ($tmp->createdDate != 0) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
    $tmp->payDate = ($tmp->payDate != 0) ? SimpleDate::fromStamp($tmp->payDate) : NULL;
    $tmp->cancelledDate = ($tmp->cancelledDate != 0) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    $tmp->confirmDate = ($tmp->confirmDate != 0) ? SimpleDate::fromStamp($tmp->confirmDate) : NULL;
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iesumall($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $months = _months(json_decode($createdFrom), json_decode($createdTo));
  foreach ($months as &$tmp) {
    $start = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, 1));
    $end = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, _days($tmp->year, $tmp->month)));
    
    $condition1 = 'WHERE 1 = 1';
    $condition1 .= ($start) ? ' AND (p.date >= \'' . $start . '\')' : '';
    $condition1 .= ($end) ? ' AND (p.date <= \'' . $end . '\')' : '';

    $condition2 = 'WHERE 1 = 1';
    $condition2 .= ($start) ? ' AND (p.payDate >= \'' . $start . '\')' : '';
    $condition2 .= ($end) ? ' AND (p.payDate <= \'' . $end . '\')' : '';
    
    $sql = "SELECT SUM(p.amount) AS result FROM $tablePayment AS p $condition1 AND p.cancelled = 0";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->receipt = ($tmp2->result) ? $tmp2->result : 0;
    }
    $sql = "SELECT SUM(p.amount) AS result FROM $tablePayment AS p $condition1 AND p.cancelled = 0 AND p.method = 'cash'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->rcash = ($tmp2->result) ? $tmp2->result : 0;
    }
    
    $sql = "SELECT SUM(p.amount) AS result FROM $tableExpense AS p $condition2 AND p.status != -1 AND p.method = 'C'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->ecash = ($tmp2->result) ? $tmp2->result : 0;
    }
    $sql = "SELECT SUM(p.amount) AS result FROM $tableExpense AS p $condition2 AND p.status != -1 AND p.method != 'C'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->enoncash = ($tmp2->result) ? $tmp2->result : 0;
    }
  }
  echo json_encode($months);
}

function iesumincome($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $months = _months(json_decode($createdFrom), json_decode($createdTo));
  foreach ($months as &$tmp) {
    $start = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, 1));
    $end = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, _days($tmp->year, $tmp->month)));
    
    $condition2 = 'WHERE 1 = 1';
    $start = max(array(SimpleDate::toStamp(json_decode($createdFrom)), $start));
    $end = min(array(SimpleDate::toStamp(json_decode($createdTo)), $end));
    $condition2 .= ($start) ? ' AND (c.createdDate >= \'' . $start . '\')' : '';
    $condition2 .= ($end) ? ' AND (c.createdDate <= \'' . $end . '\')' : '';
    
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_WEDDING . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->wedcus = ($tmp2->result) ? $tmp2->result : 0;
      $tmp->wedtol = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PREWEDDING . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->precus = ($tmp2->result) ? $tmp2->result : 0;
      $tmp->pretol = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PORTRAIT . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->porcus = ($tmp2->result) ? $tmp2->result : 0;
      $tmp->portol = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_ENGAGEMENT . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->engcus = ($tmp2->result) ? $tmp2->result : 0;
      $tmp->engtol = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_OTHER . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->othcus = ($tmp2->result) ? $tmp2->result : 0;
      $tmp->othtol = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(c.e_oid) AS result FROM $tableCustomer AS c $condition2";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->tolcus = ($tmp2->result) ? $tmp2->result : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition2 AND o.disposalstatus = 0";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tmp->toltol = ($tmp2->total) ? $tmp2->total : 0;
    }
  }
  echo json_encode($months);
}

function iesumexpense($myPdo) {
	
  global $tableExpense;
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $res = new stdClass();
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);  
  
  $condition .= ($createdFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : ''; 
  
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->expense = ($tmp->result) ? $tmp->result : 0;
  }
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1 AND d.method = 'C'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->cash = ($tmp->result) ? $tmp->result : 0;
  }
  
  $sql = "SELECT SUM(d.amount) AS result FROM $tableExpense AS d WHERE $condition AND d.status != -1 AND d.method != 'C'";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  if ($stmt->rowCount() == 1) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $res->noncash = ($tmp->result) ? $tmp->result : 0;
  }
  
  
    //$createdFrom = MiscUtils::getParam('from', NULL);
    //$createdTo = MiscUtils::getParam('to', NULL);
    
    $months = _months(json_decode($createdFrom), json_decode($createdTo));
    
      $bottom = array();
       $rest = array(); $exist;
      foreach ($months as &$tmp) {
        $start = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, 1));
        $end = SimpleDate::toStamp(SimpleDate::create($tmp->year, $tmp->month, _days($tmp->year, $tmp->month)));
        
        
        
	    $condition2 = 'WHERE 1 = 1';
	    //$start = max(array(SimpleDate::toStamp(json_decode($createdFrom)), $start));
	    //$end = min(array(SimpleDate::toStamp(json_decode($createdTo)), $end));
	    $condition2 .= ($start) ? ' AND (d.createdDate >= \'' . $start . '\')' : '';
	    $condition2 .= ($end) ? ' AND (d.createdDate <= \'' . $end . '\')' : '';        
        
        $b = new stdClass();
        $b->month = $tmp->month;
        $b->year = $tmp->year;
        $b->ecash = 0;
        $b->enoncash = 0;
        
        
          $sql2 = "SELECT e_oid,createdDate,method,amount,particular FROM $tableExpense AS d $condition2 AND d.status != -1 ";
         
		  $stmt2 = $myPdo->prepare($sql2);
		  $stmt2->execute();
		  $i = 0;
		  
		  while ($i < $stmt2->rowCount()) {
		    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
		     
		    $tmp2->id = $tmp2->e_oid;
		    $tmp2->createdDate = ($tmp2->createdDate != 0) ? SimpleDate::fromStamp($tmp2->createdDate) : NULL;
		    
		    if ($tmp2->method == 'C') {
              $b->ecash += $tmp2->amount;
            } else {
              $b->enoncash += $tmp2->amount;
            }  
            
			$exist = false;
			  //$crest = count($rest);
			  //echo $crest;
			  //echo "\n";
	        for ($j = 0, $jl = count($rest); $j < $jl; $j++) {
	          $tmp = $rest[$j];
	          if ($tmp->name == $tmp2->particular) {
	            $tmp->value += $tmp2->amount;
	            $exist = true;
	          }
	        }
	        if (!$exist) {
	          $m = new stdClass();
	          $m->name = $tmp2->particular;
	          $m->value = $tmp2->amount;
	          array_push($rest, $m);
	        }
		    
		    $i++;
		  }        
        
        array_push($bottom, $b);
      }    
  
  
      $top = new stdClass();
      $top->expense = $res->expense;
      $top->cash = $res->cash ;
      $top->noncash = $res->noncash;

      $res->top = $top;
      
      //$middle = new stdClass();
      $res->middle = $rest;  
      
      //$bottom = new stdClass();
      $res->bottom = $bottom;  
  
  echo json_encode($res);	
}

function iestatspackage($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (o.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (o.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $result = array();
  $result['wedp'] = array();
  $result['wedv'] = array();
  $result['prep'] = array();
  $result['prev'] = array();
  $result['porp'] = array();
  $result['porv'] = array();
  $result['engp'] = array();
  $result['engv'] = array();
  $result['othp'] = array();
  $result['othv'] = array();

  $sql = "SELECT p.name AS name, p.type AS type, p.orderInc AS orderInc FROM $tablePackage AS p";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp2 = new stdClass();
    $tmp2->name = $tmp->name;
    $tmp2->value = 0;
    if ($tmp->orderInc == ORDER_TYPE_WEDDING) {
      if ($tmp->type == PACKAGE_TYPE_PHOTO) {
        $result['wedp'][] = $tmp2;
      } else if ($tmp->type == PACKAGE_TYPE_VIDEO) {
        $result['wedv'][] = $tmp2;
      }
    } else if ($tmp->orderInc == ORDER_TYPE_PREWEDDING) {
      if ($tmp->type == PACKAGE_TYPE_PHOTO) {
        $result['prep'][] = $tmp2;
      } else if ($tmp->type == PACKAGE_TYPE_VIDEO) {
        $result['prev'][] = $tmp2;
      }
    } else if ($tmp->orderInc == ORDER_TYPE_PORTRAIT) {
      if ($tmp->type == PACKAGE_TYPE_PHOTO) {
        $result['porp'][] = $tmp2;
      } else if ($tmp->type == PACKAGE_TYPE_VIDEO) {
        $result['porv'][] = $tmp2;
      }
    } else if ($tmp->orderInc == ORDER_TYPE_ENGAGEMENT) {
      if ($tmp->type == PACKAGE_TYPE_PHOTO) {
        $result['engp'][] = $tmp2;
      } else if ($tmp->type == PACKAGE_TYPE_VIDEO) {
        $result['engv'][] = $tmp2;
      }
    } else if ($tmp->orderInc == ORDER_TYPE_OTHER) {
      if ($tmp->type == PACKAGE_TYPE_PHOTO) {
        $result['othp'][] = $tmp2;
      } else if ($tmp->type == PACKAGE_TYPE_VIDEO) {
        $result['othv'][] = $tmp2;
      }
    }
    $i++;
  };

  /*$sql = "SELECT o.type AS orderInc, op.var_a AS type, p.name AS name FROM $tableMyOrder AS o LEFT JOIN ns__ez_relation_myorder_myorderpackage AS op ON o.e_oid = op.oid_a LEFT JOIN $tableMyOrderPackage AS p ON p.e_oid = op.oid_b $condition";
  */
$sql = "SELECT o.type AS orderInc,o.type,o.photoname,o.videoname FROM $tableMyOrder AS o  $condition";  

  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    if ($tmp->orderInc == ORDER_TYPE_WEDDING) {
    	
      if (!empty($tmp->photoname)) {
        foreach ($result['wedp'] as &$tmp2) {
          if ($tmp2->name == $tmp->photoname) {
            $tmp2->value++;
          }
        }
      }
      
       if (!empty($tmp->videoname)) {
        foreach ($result['wedv'] as &$tmp2) {
          if ($tmp2->name == $tmp->videoname) {
            $tmp2->value++;
          }
        }
      }
      
    } else if ($tmp->orderInc == ORDER_TYPE_PREWEDDING) {
      if (!empty($tmp->photoname)) {
        foreach ($result['prep'] as &$tmp2) {
          if ($tmp2->name == $tmp->photoname) {
            $tmp2->value++;
          }
        }
      } 

      if (!empty($tmp->videoname)) {
        foreach ($result['prev'] as &$tmp2) {
          if ($tmp2->name == $tmp->videoname) {
            $tmp2->value++;
          }
        }
      }
      
    } else if ($tmp->orderInc == ORDER_TYPE_PORTRAIT) {
      if (!empty($tmp->photoname)) {
        foreach ($result['porp'] as &$tmp2) {
          if ($tmp2->name == $tmp->photoname) {
            $tmp2->value++;
          }
        }
      } 

      if (!empty($tmp->videoname)) {
        foreach ($result['porv'] as &$tmp2) {
          if ($tmp2->name == $tmp->videoname) {
            $tmp2->value++;
          }
        }
      }
      
    } else if ($tmp->orderInc == ORDER_TYPE_ENGAGEMENT) {
      
     if (!empty($tmp->photoname)) {
        foreach ($result['engp'] as &$tmp2) {
          if ($tmp2->name == $tmp->photoname) {
            $tmp2->value++;
          }
        }
      } 

      if (!empty($tmp->videoname)) {
        foreach ($result['engv'] as &$tmp2) {
          if ($tmp2->name == $tmp->videoname) {
            $tmp2->value++;
          }
        }
      }
    } else if ($tmp->orderInc == ORDER_TYPE_OTHER) {
      if (!empty($tmp->photoname)) {
        foreach ($result['othp'] as &$tmp2) {
          if ($tmp2->name == $tmp->photoname) {
            $tmp2->value++;
          }
        }
      } 

      if (!empty($tmp->videoname)) {
        foreach ($result['othv'] as &$tmp2) {
          if ($tmp2->name == $tmp->videoname) {
            $tmp2->value++;
          }
        }
      }
    }
    $i++;
  }
  echo json_encode($result);
}

function iestatsproduct($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tablePurchaseItem, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'and 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT s.e_oid AS id, s.name AS name FROM $tableSample AS s";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT COUNT(p.e_oid) AS total FROM $tablePurchaseItem AS p WHERE p.sampleId = '$tmp->id'";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $result[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iestatssource($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT s.name AS name FROM $tableISource AS s";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT COUNT(c.e_oid) AS total FROM $tableCustomer AS c WHERE c.source = '$tmp->name' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $sql2 = "SELECT SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid WHERE c.source = '".$tmp->name."' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->revenue = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->revenue = ($tmp2->total) ? $tmp2->total : 0;
    }
    $result[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iestatsculture($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT s.name AS name FROM $tableICulture AS s";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT COUNT(c.e_oid) AS total FROM $tableCustomer AS c WHERE c.culture = '".addslashes($tmp->name)."' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $sql2 = "SELECT SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid WHERE c.culture = '".addslashes($tmp->name)."' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->revenue = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->revenue = ($tmp2->total) ? $tmp2->total : 0;
    }
    $result[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iestatsreception($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT s.name AS name FROM $tableIReception AS s";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $name = addslashes($tmp->name);
    $sql2 = "SELECT COUNT(c.e_oid) AS total FROM $tableCustomer AS c WHERE c.reception = '$name' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $sql2 = "SELECT SUM(o.price) AS total FROM $tableMyOrder AS o LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid WHERE c.reception = '$name' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->revenue = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->revenue = ($tmp2->total) ? $tmp2->total : 0;
    }
    $result[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iestatsreceptioncustomers($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);


  $sql = "SELECT d.e_oid AS id FROM $tableCustomer AS d WHERE  $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $result[] = $tmp->id;
    $i++;
  }
  echo json_encode($result);	
}

function iestatsceremony($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT s.name AS name FROM $tableICeremony AS s";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $name = addslashes($tmp->name);
    $sql2 = "SELECT COUNT(c.e_oid) AS total FROM $tableCustomer AS c WHERE c.ceremony = '$name' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->value = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->value = $tmp2->total;
    }
    $sql2 = "SELECT SUM(o.price) AS total FROM $tableMyOrder AS o LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid WHERE c.ceremony = '$name' AND $condition";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $tmp->revenue = 0;
    if ($stmt2->rowCount() == 1) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $tmp->revenue = ($tmp2->total) ? $tmp2->total : 0;
    }
    $result[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function iestatsceremonycustomers($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $sql = "SELECT d.e_oid AS id FROM $tableCustomer AS d WHERE  $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();

  $result = array();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $result[] = $tmp->id;
    $i++;
  }
  echo json_encode($result);

}

function iecompbymonth($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT c.e_oid AS id, c.createdDate AS cDate FROM $tableCustomer AS c $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  $cDate = array(); $result = array();
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->orders = array();
    $sql2 = "SELECT o.type AS type, o.price AS price, o.disposalstatus AS status FROM $tableMyOrder AS o  WHERE o.customerId = $tmp->id";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $cDate[] = SimpleDate::fromStamp($tmp->cDate);
    $i++;
  }
  $years = array();
  foreach ($cDate as &$date) {
    $exist = false;
    foreach($years as &$year) {
      if ($date->year ==  $year) {
        $exist = true;
      }
    }
    if (!$exist) {
      $years[] = $date->year;
    }
  }
  $res = array();
  $a1 = array();$a2 = array();$a3 = array();$a4 = array();$a5 = array();$a6 = array();$a7 = array();$a8 = array();$a9 = array();
  $a10 = array();$a11 = array();$a12 = array();

  for ($k = 0, $kl = count($years); $k < $kl; $k++) {
    $ye = $years[$k];
    $condition1 = array();
    $tolcus = array(); $wedcus = array();$precus = array();$porcus = array();$engcus = array();$othcus = array();
    $tol = array(); $wed = array(); $pre = array(); $por = array(); $eng = array(); $oth = array();

    $condition2 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 1, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 1, _days($ye, 1))) . '\'';
    $condition3 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 2, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 2, _days($ye, 2))) . '\'';
    $condition4 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 3, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 3, _days($ye, 3))) . '\'';
    $condition5 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 4, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 4, _days($ye, 4))) . '\'';
    $condition6 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 5, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 5, _days($ye, 5))) . '\'';
    $condition7 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 6, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 6, _days($ye, 6))) . '\'';
    $condition8 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 7, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 7, _days($ye, 7))) . '\'';
    $condition9 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 8, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 8, _days($ye, 8))) . '\'';
    $condition10 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 9, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 9, _days($ye, 9))) . '\'';
    $condition11 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 10, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 10, _days($ye, 10))) . '\'';
    $condition12 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 11, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 11, _days($ye, 11))) . '\'';
    $condition13 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 12, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 12, _days($ye, 12))) . '\'';

    array_push($condition1, $condition2, $condition3, $condition4, $condition5, $condition6, $condition7, $condition8, $condition9, $condition10, $condition11, $condition12, $condition13);
    for($i = 0, $il = count($condition1); $i < $il; $i++) {
      $con = $condition1[$i];
      
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_WEDDING . "'";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $wedcus[$i] = ($tmp2->result) ? $tmp2->result : 0;
        $wed[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PREWEDDING . "'";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $precus[$i] = ($tmp2->result) ? $tmp2->result : 0;
        $pre[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PORTRAIT . "'";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $porcus[$i] = ($tmp2->result) ? $tmp2->result : 0;
        $por[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_ENGAGEMENT . "'";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $engcus[$i] = ($tmp2->result) ? $tmp2->result : 0;
        $eng[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_OTHER . "'";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $othcus[$i] = ($tmp2->result) ? $tmp2->result : 0;
        $oth[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
      $sql = "SELECT COUNT(c.e_oid) AS result FROM $tableCustomer AS c $con";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $tolcus[$i] = ($tmp2->result) ? $tmp2->result : 0;
      }
      $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $con AND o.disposalstatus = 0";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      if ($stmt->rowCount() == 1) {
        $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
        $tol[$i] = ($tmp2->total) ? $tmp2->total : 0;
      }
    }
    $ob1 = new stdClass();
    $ob1->year = $ye;
    $ob1->total = $tol[0];
    $ob1->tolcus = $tolcus[0];
    $ob1->wed = $wed[0];
    $ob1->wedcus = $wedcus[0];
    $ob1->pre = $pre[0];
    $ob1->precus = $precus[0];
    $ob1->por = $por[0];
    $ob1->porcus = $porcus[0];
    $ob1->eng = $eng[0];
    $ob1->engcus = $engcus[0];
    $ob1->oth = $oth[0];
    $ob1->othcus = $othcus[0];

    $ob2 = new stdClass();
    $ob2->year = $ye;
    $ob2->total = $tol[1];
    $ob2->tolcus = $tolcus[1];
    $ob2->wed = $wed[1];
    $ob2->wedcus = $wedcus[1];
    $ob2->pre = $pre[1];
    $ob2->precus = $precus[1];
    $ob2->por = $por[1];
    $ob2->porcus = $porcus[1];
    $ob2->eng = $eng[1];
    $ob2->engcus = $engcus[1];
    $ob2->oth = $oth[1];
    $ob2->othcus = $othcus[1];

    $ob3 = new stdClass();
    $ob3->year = $ye;
    $ob3->total = $tol[2];
    $ob3->tolcus = $tolcus[2];
    $ob3->wed = $wed[2];
    $ob3->wedcus = $wedcus[2];
    $ob3->pre = $pre[2];
    $ob3->precus = $precus[2];
    $ob3->por = $por[2];
    $ob3->porcus = $porcus[2];
    $ob3->eng = $eng[2];
    $ob3->engcus = $engcus[2];
    $ob3->oth = $oth[2];
    $ob3->othcus = $othcus[2];

    $ob4 = new stdClass();
    $ob4->year = $ye;
    $ob4->total = $tol[3];
    $ob4->tolcus = $tolcus[3];
    $ob4->wed = $wed[3];
    $ob4->wedcus = $wedcus[3];
    $ob4->pre = $pre[3];
    $ob4->precus = $precus[3];
    $ob4->por = $por[3];
    $ob4->porcus = $porcus[3];
    $ob4->eng = $eng[3];
    $ob4->engcus = $engcus[3];
    $ob4->oth = $oth[3];
    $ob4->othcus = $othcus[3];

    $ob5 = new stdClass();
    $ob5->year = $ye;
    $ob5->total = $tol[4];
    $ob5->tolcus = $tolcus[4];
    $ob5->wed = $wed[4];
    $ob5->wedcus = $wedcus[4];
    $ob5->pre = $pre[4];
    $ob5->precus = $precus[4];
    $ob5->por = $por[4];
    $ob5->porcus = $porcus[4];
    $ob5->eng = $eng[4];
    $ob5->engcus = $engcus[4];
    $ob5->oth = $oth[4];
    $ob5->othcus = $othcus[4];

    $ob6 = new stdClass();
    $ob6->year = $ye;
    $ob6->total = $tol[5];
    $ob6->tolcus = $tolcus[5];
    $ob6->wed = $wed[5];
    $ob6->wedcus = $wedcus[5];
    $ob6->pre = $pre[5];
    $ob6->precus = $precus[5];
    $ob6->por = $por[5];
    $ob6->porcus = $porcus[5];
    $ob6->eng = $eng[5];
    $ob6->engcus = $engcus[5];
    $ob6->oth = $oth[5];
    $ob6->othcus = $othcus[5];

    $ob7 = new stdClass();
    $ob7->year = $ye;
    $ob7->total = $tol[6];
    $ob7->tolcus = $tolcus[6];
    $ob7->wed = $wed[6];
    $ob7->wedcus = $wedcus[6];
    $ob7->pre = $pre[6];
    $ob7->precus = $precus[6];
    $ob7->por = $por[6];
    $ob7->porcus = $porcus[6];
    $ob7->eng = $eng[6];
    $ob7->engcus = $engcus[6];
    $ob7->oth = $oth[6];
    $ob7->othcus = $othcus[6];

    $ob8 = new stdClass();
    $ob8->year = $ye;
    $ob8->total = $tol[7];
    $ob8->tolcus = $tolcus[7];
    $ob8->wed = $wed[7];
    $ob8->wedcus = $wedcus[7];
    $ob8->pre = $pre[7];
    $ob8->precus = $precus[7];
    $ob8->por = $por[7];
    $ob8->porcus = $porcus[7];
    $ob8->eng = $eng[7];
    $ob8->engcus = $engcus[7];
    $ob8->oth = $oth[7];
    $ob8->othcus = $othcus[7];

    $ob9 = new stdClass();
    $ob9->year = $ye;
    $ob9->total = $tol[8];
    $ob9->tolcus = $tolcus[8];
    $ob9->wed = $wed[8];
    $ob9->wedcus = $wedcus[8];
    $ob9->pre = $pre[8];
    $ob9->precus = $precus[8];
    $ob9->por = $por[8];
    $ob9->porcus = $porcus[8];
    $ob9->eng = $eng[8];
    $ob9->engcus = $engcus[8];
    $ob9->oth = $oth[8];
    $ob9->othcus = $othcus[8];

    $ob10 = new stdClass();
    $ob10->year = $ye;
    $ob10->total = $tol[9];
    $ob10->tolcus = $tolcus[9];
    $ob10->wed = $wed[9];
    $ob10->wedcus = $wedcus[9];
    $ob10->pre = $pre[9];
    $ob10->precus = $precus[9];
    $ob10->por = $por[9];
    $ob10->porcus = $porcus[9];
    $ob10->eng = $eng[9];
    $ob10->engcus = $engcus[9];
    $ob10->oth = $oth[9];
    $ob10->othcus = $othcus[9];

    $ob11 = new stdClass();
    $ob11->year = $ye;
    $ob11->total = $tol[10];
    $ob11->tolcus = $tolcus[10];
    $ob11->wed = $wed[10];
    $ob11->wedcus = $wedcus[10];
    $ob11->pre = $pre[10];
    $ob11->precus = $precus[10];
    $ob11->por = $por[10];
    $ob11->porcus = $porcus[10];
    $ob11->eng = $eng[10];
    $ob11->engcus = $engcus[10];
    $ob11->oth = $oth[10];
    $ob11->othcus = $othcus[10];

    $ob12 = new stdClass();
    $ob12->year = $ye;
    $ob12->total = $tol[11];
    $ob12->tolcus = $tolcus[11];
    $ob12->wed = $wed[11];
    $ob12->wedcus = $wedcus[11];
    $ob12->pre = $pre[11];
    $ob12->precus = $precus[11];
    $ob12->por = $por[11];
    $ob12->porcus = $porcus[11];
    $ob12->eng = $eng[11];
    $ob12->engcus = $engcus[11];
    $ob12->oth = $oth[11];
    $ob12->othcus = $othcus[11];
    
    array_push($a1, $ob1);
    array_push($a2, $ob2);
    array_push($a3, $ob3);
    array_push($a4, $ob4);
    array_push($a5, $ob5);
    array_push($a6, $ob6);
    array_push($a7, $ob7);
    array_push($a8, $ob8);
    array_push($a9, $ob9);
    array_push($a10, $ob10);
    array_push($a11, $ob11);
    array_push($a12, $ob12);
  }
  $m1 = new stdClass();$m2 = new stdClass();$m3 = new stdClass();$m4 = new stdClass();$m5 = new stdClass();$m6 = new stdClass();
  $m7 = new stdClass();$m8 = new stdClass();$m9 = new stdClass();$m10 = new stdClass();$m11 = new stdClass();$m12 = new stdClass();
  $m1->month = 1;
  $m1->mains = $a1;
  $m2->month = 2;
  $m2->mains = $a2;
  $m3->month = 3;
  $m3->mains = $a3;
  $m4->month = 4;
  $m4->mains = $a4;
  $m5->month = 5;
  $m5->mains = $a5;
  $m6->month = 6;
  $m6->mains = $a6;
  $m7->month = 7;
  $m7->mains = $a7;
  $m8->month = 8;
  $m8->mains = $a8;
  $m9->month = 9;
  $m9->mains = $a9;
  $m10->month = 10;
  $m10->mains = $a10;
  $m11->month = 11;
  $m11->mains = $a11;
  $m12->month = 12;
  $m12->mains = $a12;
  array_push($res, $m1, $m2, $m3, $m4, $m5, $m6, $m7, $m8, $m9, $m10, $m11, $m12);
  echo json_encode($res);
}

function iecompbyyear($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;

  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);

  $condition .= ($createdFrom) ? ' AND (c.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (c.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';

  $sql = "SELECT c.e_oid AS id, c.createdDate AS cDate FROM $tableCustomer AS c $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  $cDate = array();
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $cDate[] = SimpleDate::fromStamp($tmp->cDate);
    $i++;
  }
  $years = array();
  foreach ($cDate as &$date) {
    $exist = false;
    foreach($years as &$year) {
      if ($date->year == $year) {
        $exist = true;
      }
    }
    if (!$exist) {
      $years[] = $date->year;
    }
  }
  $res = array();
  for ($k = 0, $kl = count($years); $k < $kl; $k++) {
    $ye = $years[$k];
    $wed = 0; $wedcus = 0; $pre = 0; $precus = 0; $por = 0; $porcus = 0; $eng = 0; $engcus = 0; $oth = 0; $othcus = 0; $total = 0; $tolcus = 0;
    
    $condition3 = 'WHERE 1 = 1 AND c.createdDate >= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 1, 1)) . '\'' . ' AND c.createdDate <= \'' . SimpleDate::toStamp(SimpleDate::create($ye, 12, 31)) . '\'';

    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_WEDDING . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $wedcus = ($tmp2->result) ? $tmp2->result : 0;
      $wed = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PREWEDDING . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $precus = ($tmp2->result) ? $tmp2->result : 0;
      $pre = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_PORTRAIT . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $porcus = ($tmp2->result) ? $tmp2->result : 0;
      $por = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_ENGAGEMENT . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $engcus = ($tmp2->result) ? $tmp2->result : 0;
      $eng = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0 AND o.type = '" . ORDER_TYPE_OTHER . "'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $othcus = ($tmp2->result) ? $tmp2->result : 0;
      $oth = ($tmp2->total) ? $tmp2->total : 0;
    }
    $sql = "SELECT COUNT(c.e_oid) AS result FROM $tableCustomer AS c $condition3";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $tolcus = ($tmp2->result) ? $tmp2->result : 0;
    }
    $sql = "SELECT COUNT(o.e_oid) AS result, SUM(o.price) AS total FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON c.e_oid = o.customerId  $condition3 AND o.disposalstatus = 0";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
    if ($stmt->rowCount() == 1) {
      $tmp2 = $stmt->fetch(PDO::FETCH_OBJ);
      $total = ($tmp2->total) ? $tmp2->total : 0;
    }
    
    $ob= new stdClass();
    $ob->year = $ye;
    $ob->total = $total;
    $ob->tolcus = $tolcus;
    $ob->wed = $wed;
    $ob->wedcus = $wedcus;
    $ob->pre = $pre;
    $ob->precus = $precus;
    $ob->por = $por;
    $ob->porcus = $porcus;
    $ob->eng = $eng;
    $ob->engcus = $engcus;
    $ob->oth = $oth;
    $ob->othcus = $othcus;
    array_push($res, $ob);
  }
  echo json_encode($res);
}

function iesumexpensedetail($myPdo) {
  global  $tableExpense;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', NULL);
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', NULL);
  $size = MiscUtils::getParam('s', NULL);
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $sql = "SELECT * FROM $tableExpense AS d WHERE  $condition AND d.status != -1";
 
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  $topResult = array(); $mainResult = array();
  $exist = false;
  
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->cancelledDate = ($tmp->cancelledDate) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    
    $exist = false;
    for ($j = 0, $jl = count($topResult); $j < $jl; $j++) {
        $t = $topResult[$j];
        if ($t->payee == $tmp->payee) {
            $t->total += $tmp->amount;
            if ($tmp->method == 'C') {
              $t->cash += $tmp->amount;
            } else {
              $t->noncash += $tmp->amount;
            }
            $exist = true;
          }
    }
        if (!$exist) {
          $obj = new stdClass();
          $obj->payee = $tmp->payee;
          $obj->total = $tmp->amount;
          $obj->cash = ($tmp->method == 'C') ? $tmp->amount : 0;
          $obj->noncash = ($tmp->method == 'C') ? 0 : $tmp->amount;
          array_push($topResult, $obj);
        }
            
    //$result->data[] = $tmp;
    $i++;
  }

      $res = new stdClass();
      $res->top = $topResult;
      echo json_encode($res);	
}

function deletecus($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT DISTINCT c.e_oid AS total FROM $tableCustomer AS c WHERE c.cancelled = 1 AND $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();

  $sql = "SELECT DISTINCT c.e_oid AS id, c.admAppr as admAppr, c.cancelledDate as cancelledDate, c.cancelledOperator as cancelledOperator, c.cancelledMessage as cancelledMessage FROM $tableCustomer AS c WHERE c.cancelled = 1 AND $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->cancelledDate = ($tmp->cancelledDate) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function deleteorder($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 1 and o.customerId != '0' AND $condition";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();

  $sql2 = "SELECT DISTINCT o.e_oid AS id, o.admAppr as admAppr, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.disposalmessage as cancelledMessage, o.disposaluserAccount as cancelledOperator, o.disposaldate as cancelledDate FROM $tableMyOrder AS o LEFT JOIN  $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 1 and o.customerId != '0'  AND $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    $tmp2->cancelledDate = ($tmp2->cancelledDate) ? SimpleDate::fromStamp($tmp2->cancelledDate) : NULL;
    $result->data[] = $tmp2;
    $j++;
  }
  echo json_encode($result);
}

function orderdiscount($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  $condition  AND o.disposalstatus = 0 AND o.discount>20";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();

  $sql2 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.discount as discount, o.isOrderDiscountSolved as isOrderDiscountSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   $condition  AND o.disposalstatus = 0 AND o.discount>20 ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.orderDiscountNote AS orderDiscountNote FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp2->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp2->note = $tmp4->orderDiscountNote;
      $l++;
    }
    
    $result->data[] = $tmp2;
    $j++;
  }
  echo json_encode($result);
}

function noworkingdateorder($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId FROM $tableMyOrder AS o  JOIN $tableCustomer AS c ON o.customerId = c.e_oid   $condition  AND o.disposalstatus = 0 AND o.date = 0";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();

  $sql2 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isNoWeddingDaySolved as isNoWeddingDaySolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   $condition  AND o.disposalstatus = 0  AND o.date = 0 ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    $tmp2->createdDate = SimpleDate::fromStamp($tmp2->createdDate);
    $tmp2->date = SimpleDate::fromStamp($tmp2->date);
    $tmp2->payment = 0;
    $sql3 = "SELECT DISTINCT p.e_oid AS id, p.amount AS amount FROM $tablePayment AS p  WHERE p.orderId = $tmp2->id AND p.cancelled = 0";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $k = 0;
    while ($k < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp2->payment += $tmp3->amount;
      $k++;
    }
    
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.noWeddingDayNote AS noWeddingDayNote FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp2->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp2->note = $tmp4->noWeddingDayNote;
      $l++;
    }
    $result->data[] = $tmp2;
    $j++;
  }
  
  echo json_encode($result);
}


function firstpayment1($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment1FirstSolved as isPayment1FirstSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition ORDER BY $order $queue";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0; $count = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp->id AND p.cancelled = 0 AND p.stage = 1";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0; $total = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $j++;
    }
    $tmp->createdDate = SimpleDate::fromStamp($tmp->createdDate);
    $tmp->date = SimpleDate::fromStamp($tmp->date);
    $tmp->payment = $total;
    
    $sql3 = "SELECT DISTINCT n.e_oid AS id, n.firstPaymentNote1 AS firstPaymentNote1 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp->id";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $k = 0;
    while ($k < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $tmp->note = $tmp3->firstPaymentNote1;
      $k++;
    }
    
    if ((($total / $tmp->price) > 0 && ($total / $tmp->price) < 0.1)) {
      $result->data[] = $tmp;
    }
    $i++;
  }
  echo json_encode($result);
}

function firstpayment2($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment1SecondSolved as isPayment1SecondSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 30) . ") ORDER BY $order $queue";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp->id AND p.cancelled = 0 AND p.stage = 1";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    $tmp->createdDate = SimpleDate::fromStamp($tmp->createdDate);
    $tmp->date = SimpleDate::fromStamp($tmp->date);
    $tmp->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.firstPaymentNote2 AS firstPaymentNote2 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp->note = $tmp4->firstPaymentNote2;
      $l++;
    }
    if ($total == 0) {
      $result->data[] = $tmp;
    }
    $k++;
  }
  echo json_encode($result);
}

function firstpayment3 ($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment1ThirdSolved as isPayment1ThirdSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 7) . ") ORDER BY $order $queue";
  $stmt3 = $myPdo->prepare($sql3);
  $stmt3->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 1";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
    $tmp3->date = SimpleDate::fromStamp($tmp3->date);
    $tmp3->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.firstPaymentNote3 AS firstPaymentNote3 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp3->note = $tmp4->firstPaymentNote3;
      $l++;
    }
    
    if ($total == 0) {
      $result->data[] = $tmp3;
    }
    $k++;
  }
  echo json_encode($result);
}

function firstpayment4($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment1ForthSolved as isPayment1ForthSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition AND o.date != 0 AND (o.date <" . (time()) . ") ORDER BY $order $queue";
  $stmt3 = $myPdo->prepare($sql3);
  $stmt3->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 1";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
    $tmp3->date = SimpleDate::fromStamp($tmp3->date);
    $tmp3->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.firstPaymentNote4 AS firstPaymentNote4 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp3->note = $tmp4->firstPaymentNote4;
      $l++;
    }
    if ($total == 0) {
      $result->data[] = $tmp3;
    }
    $k++;
  }
  echo json_encode($result);
}


function secondpayment1 ($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;  
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment2FirstSolved as isPayment2FirstSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 7) . ") ORDER BY $order $queue";
  $stmt3 = $myPdo->prepare($sql3);
  $stmt3->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 2";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    
    $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
    $tmp3->date = SimpleDate::fromStamp($tmp3->date);
    $tmp3->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.secondPaymentNote1 AS secondPaymentNote1 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp3->note = $tmp4->secondPaymentNote1;
      $l++;
    }
    if ($total == 0) {
      $result->data[] = $tmp3;
    }
    $k++;
  }
  echo json_encode($result);
}

function secondpayment2($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment2SecondSolved as isPayment2SecondSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $condition AND o.date != 0 AND (o.date <" . (time()) . ") ORDER BY $order $queue";
  $stmt3 = $myPdo->prepare($sql3);
  $stmt3->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 2";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    
    $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
    $tmp3->date = SimpleDate::fromStamp($tmp3->date);
    $tmp3->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.secondPaymentNote2 AS secondPaymentNote2 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp3->note = $tmp4->secondPaymentNote2;
      $l++;
    }
    if ($total == 0) {
      $result->data[] = $tmp3;
    }
    $k++;
  }
  echo json_encode($result);
}

function thirdpayment1($myPdo, $myPdo1) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProducePurchaseItem, $tableProduceTrack, $tableProduceTransition, $tableProduceUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql5 = "SELECT DISTINCT pc.orderId as orderId FROM $tableProduce AS pc WHERE pc.cancelled = 0 AND pc.completed = 0 AND pc.pi_model='Photographer' AND pc.state <= 5 and pc.branch='".BRANCH."'";
  $stmt5 = $myPdo1->prepare($sql5);
  $stmt5->execute();
  $f = 0;
  while ($f < $stmt5->rowCount()) {
    $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
    
    $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment3FirstSolved as isPayment3FirstSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.e_oid = $tmp5->orderId AND o.disposalstatus = 0 AND o.finished = 0 AND o.type = 'Wedding' AND o.price != 0 AND o.date != 0 $condition AND (o.date <=" . (time() - 86400 * 30) . ") ORDER BY $order $queue";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $k = 0; $cout = 0;
    while ($k < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      
      $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 3";
      $stmt2 = $myPdo->prepare($sql2);
      $stmt2->execute();
      $m = 0; $total = 0;
      while ($m < $stmt2->rowCount()) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $total += $tmp2->amount;
        $m++;
      }
      
      $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
      $tmp3->date = SimpleDate::fromStamp($tmp3->date);
      $tmp3->payment = $total;
    
      $sql4 = "SELECT DISTINCT n.e_oid AS id, n.thirdPaymentNote1 AS thirdPaymentNote1 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
      $stmt4 = $myPdo->prepare($sql4);
      $stmt4->execute();
      $l = 0;
      while ($l < $stmt4->rowCount()) {
        $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
        $tmp3->note = $tmp4->thirdPaymentNote1;
        $l++;
      }
      $result->data[] = $tmp3;
      $k++;
    }
    $f++;
  }
  echo json_encode($result);
}

function thirdpayment2($myPdo, $myPdo1) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql5 = "SELECT DISTINCT pc.orderId as orderId FROM $tableProduce AS pc WHERE pc.cancelled = 0 AND pc.completed = 0 AND pc.pi_model='Photographer' AND pc.state > 5  and pc.branch='".BRANCH."'";
  $stmt5 = $myPdo1->prepare($sql5);
  $stmt5->execute();
  $f = 0;
  while ($f < $stmt5->rowCount()) {
    $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
   
    $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment3SecondSolved as isPayment3SecondSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.e_oid = $tmp5->orderId AND o.disposalstatus = 0 AND o.finished = 0 AND o.type = 'Wedding' AND o.price != 0 $condition  ORDER BY $order $queue";
    $stmt3 = $myPdo->prepare($sql3);
    $stmt3->execute();
    $k = 0; $cout = 0;
    while ($k < $stmt3->rowCount()) {
      $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
      $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 3";
      $stmt2 = $myPdo->prepare($sql2);
      $stmt2->execute();
      $m = 0; $total = 0;
      while ($m < $stmt2->rowCount()) {
        $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
        $total += $tmp2->amount;
        $m++;
      }
      
      $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
      $tmp3->date = SimpleDate::fromStamp($tmp3->date);
      $tmp3->payment = $total;
    
      $sql4 = "SELECT DISTINCT n.e_oid AS id, n.thirdPaymentNote2 AS thirdPaymentNote2 FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
      $stmt4 = $myPdo->prepare($sql4);
      $stmt4->execute();
      $l = 0;
      while ($l < $stmt4->rowCount()) {
        $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
        $tmp3->note = $tmp4->thirdPaymentNote2;
        $l++;
      }
      if ($total == 0) {
        $result->data[] = $tmp3;
      }
      $k++;
    }
    $f++;
  }
  echo json_encode($result);
}

function forthpayment($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql3 = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment4Solved as isPayment4Solved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 1 AND o.type = 'ORDER_TYPE_WEDDING' AND o.price != 0 AND o.date != 0 $condition ORDER BY $order $queue";
  $stmt3 = $myPdo->prepare($sql3);
  $stmt3->execute();
  $k = 0; $cout = 0;
  while ($k < $stmt3->rowCount()) {
    $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 4";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $m = 0; $total = 0;
    while ($m < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $m++;
    }
    
    $tmp3->createdDate = SimpleDate::fromStamp($tmp3->createdDate);
    $tmp3->date = SimpleDate::fromStamp($tmp3->date);
    $tmp3->payment = $total;
  
    $sql4 = "SELECT DISTINCT n.e_oid AS id, n.forthPaymentNote AS forthPaymentNote FROM $tableMyOrderNote AS n WHERE n.orderId = $tmp3->id";
    $stmt4 = $myPdo->prepare($sql4);
    $stmt4->execute();
    $l = 0;
    while ($l < $stmt4->rowCount()) {
      $tmp4 = $stmt4->fetch(PDO::FETCH_OBJ);
      $tmp3->note = $tmp4->forthPaymentNote;
      $l++;
    }
    if ($total == 0) {
      $result->data[] = $tmp3;
    }
    $k++;
  }
  echo json_encode($result);
}

function noweddingday($myPdo) {
  global $tableMyOrder;

  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$order) {
    $sql = "UPDATE $tableMyOrder set isNoWeddingDaySolved = 1 WHERE e_oid = '$order'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function noweddingday1($myPdo) {
  global $tableMyOrder;

  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$order) {
    $sql = "UPDATE $tableMyOrder set isNoWeddingDaySolved = 2 WHERE e_oid = '$order'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function paymentproblems($myPdo) {
  global $tableMyOrder;

  $orders = json_decode(MiscUtils::getParam('o', NULL));
  $cond = json_decode(MiscUtils::getParam('c', NULL));
  
  foreach($orders as &$order) {
    if ($cond == 1) {
      $sql = "UPDATE $tableMyOrder set isPayment1FirstSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 2) {
      $sql = "UPDATE $tableMyOrder set isPayment1SecondSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 3) {
      $sql = "UPDATE $tableMyOrder set isPayment1ThirdSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 4) {
      $sql = "UPDATE $tableMyOrder set isPayment1ForthSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 5) {
      $sql = "UPDATE $tableMyOrder set isPayment2FirstSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 6) {
      $sql = "UPDATE $tableMyOrder set isPayment2SecondSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 7) {
      $sql = "UPDATE $tableMyOrder set isPayment3FirstSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 8) {
      $sql = "UPDATE $tableMyOrder set isPayment3SecondSolved = 1 WHERE e_oid = '$order'";
    } else if ($cond == 9) {
      $sql = "UPDATE $tableMyOrder set isPayment4Solved = 1 WHERE e_oid = '$order'";
    }
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function paymentproblems1($myPdo) {
  global $tableMyOrder;

  $orders = json_decode(MiscUtils::getParam('o', NULL));
  $cond = json_decode(MiscUtils::getParam('c', NULL));
  
  foreach($orders as &$order) {
    if ($cond == 1) {
      $sql = "UPDATE $tableMyOrder set isPayment1FirstSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 2) {
      $sql = "UPDATE $tableMyOrder set isPayment1SecondSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 3) {
      $sql = "UPDATE $tableMyOrder set isPayment1ThirdSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 4) {
      $sql = "UPDATE $tableMyOrder set isPayment1ForthSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 5) {
      $sql = "UPDATE $tableMyOrder set isPayment2FirstSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 6) {
      $sql = "UPDATE $tableMyOrder set isPayment2SecondSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 7) {
      $sql = "UPDATE $tableMyOrder set isPayment3FirstSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 8) {
      $sql = "UPDATE $tableMyOrder set isPayment3SecondSolved = 2 WHERE e_oid = '$order'";
    } else if ($cond == 9) {
      $sql = "UPDATE $tableMyOrder set isPayment4Solved = 2 WHERE e_oid = '$order'";
    }
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function smryadmin($myPdo, $myPdo1) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  global $tableProduce, $tableProduceEvent, $tableProduceItemMail, $tableProduceItemUser, $tableProduceProduct, $tableProduceProductSelect, $tableProducePurchase, $tableProduceTrack, $tableProduceTransition, $tableProduceUser, $tableProducePurchaseItem;
  
  $args = MiscUtils::getParam('a', NULL);
  $args = ($args) ? json_decode($args) : array();

  $smry = array();
  
  foreach ($args as &$arg) {
    try {
      $smry[$arg->name] = array();
      if ($arg->cond == 1) {
        $sql = "SELECT DISTINCT o.e_oid AS id, o.price as price FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition";
        $stmt = $myPdo->prepare($sql);
        $stmt->execute();
        $i = 0; $count1 = 0;
        while ($i < $stmt->rowCount()) {
          $tmp = $stmt->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp->id AND p.cancelled = 0 AND p.stage = 1";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $j = 0; $total = 0;
          while ($j < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $j++;
          }
          if ((($total / $tmp->price) > 0 && ($total / $tmp->price) < 0.1)) {
            $count1++;
          }
          $i++;
        }
        $smry[$arg->name]['total'] = $count1;
      } else if ($arg->cond == 2) {
        $sql = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 30) . ")";
        $stmt = $myPdo->prepare($sql);
        $stmt->execute();
        $k = 0; $count2 = 0;
        while ($k < $stmt->rowCount()) {
          $tmp = $stmt->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp->id AND p.cancelled = 0 AND p.stage = 1";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count2++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count2;
      } else if ($arg->cond == 3) {
        $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 7) . ")";
        $stmt3 = $myPdo->prepare($sql3);
        $stmt3->execute();
        $k = 0; $count3 = 0;
        while ($k < $stmt3->rowCount()) {
          $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 1";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count3++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count3;
      } else if ($arg->cond == 4) {
        $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition AND o.date != 0 AND (o.date <" . (time()) . ")";
        $stmt3 = $myPdo->prepare($sql3);
        $stmt3->execute();
        $k = 0; $count4 = 0;
        while ($k < $stmt3->rowCount()) {
          $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 1";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count4++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count4;
      } else if ($arg->cond == 5) {
        $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition AND (o.date >" . (time()) . " AND o.date <" . (time() + 86400 * 7) . ")";
        $stmt3 = $myPdo->prepare($sql3);
        $stmt3->execute();
        $k = 0; $count5 = 0;
        while ($k < $stmt3->rowCount()) {
          $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 2";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count5++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count5;
      } else if ($arg->cond == 6) {
        $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.price != 0 $arg->condition AND o.date != 0 AND (o.date <" . (time()) . ")";
        $stmt3 = $myPdo->prepare($sql3);
        $stmt3->execute();
        $k = 0; $count6 = 0;
        while ($k < $stmt3->rowCount()) {
          $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 2";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count6++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count6;
      } else if ($arg->cond == 7) {
        $sql5 = "SELECT DISTINCT pc.orderId as orderId FROM $tableProduce AS pc WHERE pc.cancelled = 0 AND pc.completed = 0 AND pc.pi_model='Photographer' AND pc.state <= 5 and pc.branch='".BRANCH."'";
        $stmt5 = $myPdo1->prepare($sql5);
        $stmt5->execute();
        $f = 0; $count7 = 0;
        while ($f < $stmt5->rowCount()) {
          $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
          $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.e_oid = $tmp5->orderId AND o.disposalstatus = 0 AND o.finished = 0 AND o.type = 'Wedding' AND o.price != 0 $arg->condition AND o.date != 0 AND (o.date <=" . (time() - 86400 * 30) . ")";
          $stmt3 = $myPdo->prepare($sql3);
          $stmt3->execute();
          $k = 0; 
          while ($k < $stmt3->rowCount()) {
            $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
            $count7++;
            $k++;
          }
          $f++;
        }
        $smry[$arg->name]['total'] = $count7;
      } else if ($arg->cond == 8) {
        $sql5 = "SELECT DISTINCT pc.orderId as orderId FROM $tableProduce AS pc WHERE pc.cancelled = 0 AND pc.completed = 0 AND pc.pi_model='Photographer' AND pc.state > 5 and pc.branch='".BRANCH."'";
        $stmt5 = $myPdo1->prepare($sql5);
        $stmt5->execute();
        $f = 0; $count8 = 0;
        while ($f < $stmt5->rowCount()) {
          $tmp5 = $stmt5->fetch(PDO::FETCH_OBJ);
         
          $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.e_oid = $tmp5->orderId AND o.disposalstatus = 0 AND o.finished = 0 AND o.type = 'Wedding' AND o.price != 0 $arg->condition";
          $stmt3 = $myPdo->prepare($sql3);
          $stmt3->execute();
          $k = 0;
          while ($k < $stmt3->rowCount()) {
            $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
            $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 3";
            $stmt2 = $myPdo->prepare($sql2);
            $stmt2->execute();
            $m = 0; $total = 0;
            while ($m < $stmt2->rowCount()) {
              $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
              $total += $tmp2->amount;
              $m++;
            }
            if ($total == 0) {
              $count8++;
            }
            $k++;
          }
          $f++;
        }
        $smry[$arg->name]['total'] = $count8;
      } else if ($arg->cond == 9) {
        $sql3 = "SELECT DISTINCT o.e_oid AS id FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid  WHERE  o.disposalstatus = 0 AND o.finished = 1 AND o.type = 'ORDER_TYPE_WEDDING' AND o.price != 0 AND o.date != 0 $arg->condition";
        $stmt3 = $myPdo->prepare($sql3);
        $stmt3->execute();
        $k = 0; $count9 = 0;
        while ($k < $stmt3->rowCount()) {
          $tmp3 = $stmt3->fetch(PDO::FETCH_OBJ);
          $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp3->id AND p.cancelled = 0 AND p.stage = 4";
          $stmt2 = $myPdo->prepare($sql2);
          $stmt2->execute();
          $m = 0; $total = 0;
          while ($m < $stmt2->rowCount()) {
            $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
            $total += $tmp2->amount;
            $m++;
          }
          if ($total == 0) {
            $count9++;
          }
          $k++;
        }
        $smry[$arg->name]['total'] = $count9;
      }
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
  echo json_encode($smry);
}

function paymentbonus($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 8);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT o.e_oid AS id, o.flag AS flag, c.e_oid as customerId, o.trackId as trackId, o.createdDate as createdDate, o.date as date, o.price as price, o.isPayment1FirstSolved as isPayment1FirstSolved FROM $tableMyOrder AS o  LEFT JOIN $tableCustomer AS c ON o.customerId = c.e_oid   WHERE  o.disposalstatus = 0 AND o.finished = 0 AND o.isBonusPaid = 0 ORDER BY $order $queue";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0; $count = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $sql2 = "SELECT DISTINCT p.e_oid AS id, p.amount as amount FROM $tablePayment AS p  WHERE p.orderId = $tmp->id AND p.cancelled = 0";
    $stmt2 = $myPdo->prepare($sql2);
    $stmt2->execute();
    $j = 0; $total = 0;
    while ($j < $stmt2->rowCount()) {
      $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
      $total += $tmp2->amount;
      $j++;
    }
    $tmp->createdDate = SimpleDate::fromStamp($tmp->createdDate);
    $tmp->date = SimpleDate::fromStamp($tmp->date);
    $tmp->payment = $total;
    
    if ($tmp->price != 0 && ($total / $tmp->price) > 0.3) {
      $result->data[] = $tmp;
    }
    $i++;
  }
  echo json_encode($result);
}

function bonuspaid($myPdo) {
  global $tableMyOrder;

  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$order) {
    $sql = "UPDATE $tableMyOrder set isBonusPaid = 1 WHERE e_oid = '$order'"; 
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function approvecus($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;

  $customers = json_decode(MiscUtils::getParam('c', NULL));

  foreach($customers as &$id) {
    $sql = "UPDATE $tableCustomer set admAppr = 1 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function approveorder($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$order) {
    $sql = "UPDATE $tableMyOrder set admAppr = 1 WHERE e_oid = '$order'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function approveexpense($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $expense = json_decode(MiscUtils::getParam('o', NULL));

  foreach($expense as &$id) {
    $sql = "UPDATE $tableExpense set admAppr = 1 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function approvepayment($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $payment = json_decode(MiscUtils::getParam('o', NULL));
  
  print_r($payment);

  foreach($payment as &$id) {
    $sql = "UPDATE $tablePayment set status = 3 WHERE e_oid = '$id'";
    
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function findByRequest($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'c.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 50);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = MiscUtils::getParam('sql', '');
  if ($sql) {
    try {
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $i = 0; $count = 0;
      while ($i < $stmt->rowCount()) {
        $tmp = $stmt->fetch(PDO::FETCH_OBJ);
        $result->data[] = $tmp;
        $i++;
      }
      echo json_encode($result);
    } catch (PDOException $e) {
      echo $e->getMessage();
    }
  }
}

function weeklywages($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', '1 = 1');
  $order = MiscUtils::getParam('o', 'e.e_oid');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 99999);
  $pageSkip = ($page - 1) * $size;

  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql2 = "SELECT DISTINCT e.e_oid AS id, e.payDate as date, e.amount as amount FROM $tableExpense AS e WHERE $condition";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    $tmp2->date = ($tmp2->date) ? SimpleDate::fromStamp($tmp2->date) : NULL;
    $result->data[] = $tmp2;
    $j++;
  }
  echo json_encode($result);
}

function paymentpd($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $payment = json_decode(MiscUtils::getParam('o', NULL));

  foreach($payment as &$id) {
    $sql = "UPDATE $tablePayment set isPdSolved = 2 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function paymentpd1($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $payment = json_decode(MiscUtils::getParam('o', NULL));

  foreach($payment as &$id) {
    $sql = "UPDATE $tablePayment set isPdSolved = 1 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql);
    $stmt->execute();
  }
  echo json_encode(array());
}

function discount($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$id) {
    $sql = "UPDATE $tableMyOrder set isOrderDiscountSolved = 2 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function discount1($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tableMyOrderNote, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $orders = json_decode(MiscUtils::getParam('o', NULL));

  foreach($orders as &$id) {
    $sql = "UPDATE $tableMyOrder set isOrderDiscountSolved = 1 WHERE e_oid = '$id'";
    $stmt = $myPdo->prepare($sql); 
    $stmt->execute();
  }
  echo json_encode(array());
}

function wageupdated($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $order = MiscUtils::getParam('o', 'e.confirmDate');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 6);
  $pageSkip = ($page - 1) * $size;
  
  $operator = json_decode(MiscUtils::getParam('operator', NULL));
  
  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;

  $sql2 = "SELECT DISTINCT e.e_oid AS id, e.payee as payee, e.particular as particular, e.amount as amount, e.note as note, e.confirmOperator as confirmOperator, e.confirmDate as confirmDate, e.status as status FROM $tableExpense AS e $condition AND e.confirmOperator = '$operator->account' ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    $tmp2->confirmDate = ($tmp2->confirmDate) ? SimpleDate::fromStamp($tmp2->confirmDate) : NULL;
    $result->data[] = $tmp2;
    $j++;
  }
  echo json_encode($result);
}

function wagesupdated($myPdo) {
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
  
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', ' 1 = 1 ');
  $order = MiscUtils::getParam('o', 'e.confirmDate');
  $queue = MiscUtils::getParam('q', 'DESC');
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', 20);
  $pageSkip = ($page - 1) * $size;
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  $cond = MiscUtils::getParam('con', NULL);

  $condition .= ($createdFrom) ? ' AND (e.confirmDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (e.confirmDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  
  $result = new stdClass();
  $result->data = array();
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  
  $sql = "SELECT DISTINCT e.e_oid AS id FROM $tableExpense AS e WHERE $condition AND $cond";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql2 = "SELECT DISTINCT e.e_oid AS id, e.payee as payee, e.particular as particular, e.amount as amount, e.note as note, e.confirmOperator as confirmOperator, e.confirmDate as confirmDate, e.status as status FROM $tableExpense AS e WHERE $condition AND $cond ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt2 = $myPdo->prepare($sql2);
  $stmt2->execute();
  $j = 0;
  while ($j < $stmt2->rowCount()) {
    $tmp2 = $stmt2->fetch(PDO::FETCH_OBJ);
    $tmp2->confirmDate = ($tmp2->confirmDate) ? SimpleDate::fromStamp($tmp2->confirmDate) : NULL;
    $result->data[] = $tmp2;
    $j++;
  }
  echo json_encode($result);
}

function deletepayment($myPdo)
{
	  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
	  
	  $function = MiscUtils::getParam('f', NULL);
	  $condition = MiscUtils::getParam('c', '1 = 1');
	  $order = MiscUtils::getParam('o', 'd.e_oid');
	  $queue = MiscUtils::getParam('q', 'DESC');
	  $page = MiscUtils::getParam('p', START);
	  $size = MiscUtils::getParam('s', 8);
	  $pageSkip = ($page - 1) * $size;
	
	  $result = new stdClass();
	  $result->data = array();
	  $result->page = $page;
	  $result->size = $size;
	  $result->order = $order;
	  $result->queue = $queue;
	  $result->condition = $condition;
	
	  $sql = "SELECT DISTINCT d.e_oid AS total FROM $tablePayment AS d WHERE d.cancelled = 1 AND $condition";
	  $stmt = $myPdo->prepare($sql);
	  $stmt->execute();
	  $result->total = $stmt->rowCount();
	
	  $sql = "SELECT * FROM $tablePayment AS d WHERE d.cancelled = 1 AND $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
	  
	  $stmt = $myPdo->prepare($sql);
	  $stmt->execute();
	  $i = 0;
	  while ($i < $stmt->rowCount()) {
	    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
	    $tmp->id = $tmp->e_oid;
	    $tmp->createdDate = ($tmp->createdDate) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
	    $tmp->date = ($tmp->date) ? SimpleDate::fromStamp($tmp->date) : NULL;
	    $tmp->cancelledDate = ($tmp->cancelledDate) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
	    $result->data[] = $tmp;
	    $i++;
	  }
	  echo json_encode($result);	
}


function deleteexpenses($myPdo)
{
      global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
      
      $function = MiscUtils::getParam('f', NULL);
      $condition = MiscUtils::getParam('c', '1 = 1');
      $order = MiscUtils::getParam('o', 'd.e_oid');
      $queue = MiscUtils::getParam('q', 'DESC');
      $page = MiscUtils::getParam('p', START);
      $size = MiscUtils::getParam('s', 8);
      $pageSkip = ($page - 1) * $size;
    
      $result = new stdClass();
      $result->data = array();
      $result->page = $page;
      $result->size = $size;
      $result->order = $order;
      $result->queue = $queue;
      $result->condition = $condition;
    
      $sql = "SELECT DISTINCT d.e_oid AS total FROM $tableExpense AS d WHERE 1 = 1 AND $condition";
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $result->total = $stmt->rowCount();
    
      $sql = "SELECT * FROM $tableExpense AS d WHERE 1 = 1 AND $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
      
      $stmt = $myPdo->prepare($sql);
      $stmt->execute();
      $i = 0;
      while ($i < $stmt->rowCount()) {
        $tmp = $stmt->fetch(PDO::FETCH_OBJ);
        $tmp->id = $tmp->e_oid;
        $tmp->createdDate = ($tmp->createdDate) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
        $tmp->payDate = ($tmp->payDate) ? SimpleDate::fromStamp($tmp->payDate) : NULL;
        $tmp->cancelledDate = ($tmp->cancelledDate) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
        $result->data[] = $tmp;
        $i++;
      }
      echo json_encode($result);    
}

function payments($myPdo) {
global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
      	
	
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.date');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;  
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $pFrom = MiscUtils::getParam('pfrom', NULL);
  $pTo = MiscUtils::getParam('pto', NULL);  
  
  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $condition .= ($pFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($pFrom)) . '\')' : '';
  $condition .= ($pTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($pTo)) . '\')' : '';  
  
  $result = new stdClass();
  $result->data = array(); 
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;    
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->createdDate = ($tmp->createdDate != 0) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
    $tmp->date = ($tmp->date != 0) ? SimpleDate::fromStamp($tmp->date) : NULL;
    $tmp->cancelledDate = ($tmp->cancelledDate != 0) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);
}

function expenses($myPdo)
{
  global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
	
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.createdDate');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;  
  
//  $createdFrom = MiscUtils::getParam('cfrom', NULL);
//  $createdTo = MiscUtils::getParam('cto', NULL);
//  $paydateFrom = MiscUtils::getParam('pfrom', NULL);
//  $paydateTo = MiscUtils::getParam('pto', NULL);  
  
  $status = MiscUtils::getParam('ss', NULL);
  
//  $condition .= ($createdFrom) ? ' AND (d.createdDate >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
//  $condition .= ($createdTo) ? ' AND (d.createdDate <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
//  $condition .= ($paydateFrom) ? ' AND (d.payDate >= \'' . SimpleDate::toStamp(json_decode($paydateFrom)) . '\')' : '';
//  $condition .= ($paydateTo) ? ' AND (d.payDate <= \'' . SimpleDate::toStamp(json_decode($paydateTo)) . '\')' : '';  
  
  $condition .= ($status) ? ' AND (d.status != \'-1\')' : '';
  
//  echo $condition;
  
  $result = new stdClass();
  $result->data = array(); 
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;    
  
  $sql = "SELECT * FROM $tableExpense AS d WHERE $condition";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tableExpense AS d WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->createdDate = ($tmp->createdDate != 0) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
    $tmp->payDate = ($tmp->payDate != 0) ? SimpleDate::fromStamp($tmp->payDate) : NULL;
    $tmp->cancelledDate = ($tmp->cancelledDate != 0) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    $tmp->confirmDate = ($tmp->confirmDate != 0) ? SimpleDate::fromStamp($tmp->confirmDate) : NULL;
    $result->data[] = $tmp;
    $i++;
  }
  echo json_encode($result);	
}

function acctpayment($myPdo)
{
  global  $tablePayment;
        
    
  $function = MiscUtils::getParam('f', NULL);
  $condition = MiscUtils::getParam('c', NULL);
  $order = MiscUtils::getParam('o', 'd.date');
  $queue = MiscUtils::getParam('q', NULL);
  $page = MiscUtils::getParam('p', START);
  $size = MiscUtils::getParam('s', INFINITE);
  $pageSkip = ($page - 1) * $size;  
  
  $createdFrom = MiscUtils::getParam('from', NULL);
  $createdTo = MiscUtils::getParam('to', NULL);
  
  $condition .= ($createdFrom) ? ' AND (d.date >= \'' . SimpleDate::toStamp(json_decode($createdFrom)) . '\')' : '';
  $condition .= ($createdTo) ? ' AND (d.date <= \'' . SimpleDate::toStamp(json_decode($createdTo)) . '\')' : '';
  
  $res = new stdClass();
  $result = new stdClass();
  $result->data = array(); 
  $result->page = $page;
  $result->size = $size;
  $result->order = $order;
  $result->queue = $queue;
  $result->condition = $condition;
  $res->condition = $condition;    
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition";
  
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $result->total = $stmt->rowCount();
  
  $sql = "SELECT * FROM $tablePayment AS d WHERE $condition ORDER BY $order $queue LIMIT $pageSkip, $size";
  $stmt = $myPdo->prepare($sql);
  $stmt->execute();
  $i = 0;
   $receipt = 0; $cash = 0; $noncash = 0;
  while ($i < $stmt->rowCount()) {
    $tmp = $stmt->fetch(PDO::FETCH_OBJ);
    $tmp->id = $tmp->e_oid;
    $tmp->createdDate = ($tmp->createdDate != 0) ? SimpleDate::fromStamp($tmp->createdDate) : NULL;
    $tmp->date = ($tmp->date != 0) ? SimpleDate::fromStamp($tmp->date) : NULL;
    $tmp->cancelledDate = ($tmp->cancelledDate != 0) ? SimpleDate::fromStamp($tmp->cancelledDate) : NULL;
    
        $receipt += $tmp->amount;
        if ($tmp->method == 'cash') {
          $cash += $tmp->amount;
        } else {
          $noncash += $tmp->amount;
        }    
    
    //$result->data[] = $tmp;
    $i++;
  }
  
      $top = new stdClass();
      $top->receipt = $receipt;
      $top->cash = $cash;
      $top->noncash = $noncash;
      
      
      $res->top = $top;
      echo json_encode($res);  
  
     
     /* for ($i = 0, $il = count($result->data); $i < $il; $i++) {
        $payment = $result->data[$i];
        if ($payment->cancelled) {
          continue;
        }
        $receipt += $payment->amount;
        if ($payment->method == 'cash') {
          $cash += $payment->amount;
        } else {
          $noncash += $payment->amount;
        }
      }
      $top = new stdClass();
      $top->receipt = $receipt;
      $top->cash = $cash;
      $top->noncash = $noncash;
      
      $res = new stdClass();
      $res->top = $top;
      echo json_encode($res);  
   */
  //echo json_encode($result);
  	
}



?>